package com.sxpcwlkj.sxpcwlkj.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sxpcwlkj.common.annotation.MssSafety;
import com.sxpcwlkj.common.code.controller.BaseController;
import com.sxpcwlkj.common.utils.MapstructUtil;
import com.sxpcwlkj.common.utils.R;
import com.sxpcwlkj.common.code.entity.PrintObject;
import com.sxpcwlkj.datasource.entity.page.TableDataInfo;
import com.sxpcwlkj.framework.config.ValidatedGroupConfig;
import com.sxpcwlkj.framework.utils.ExcelUtil;
import com.sxpcwlkj.sxpcwlkj.entity.bo.StoreMemberAddressBo;
import com.sxpcwlkj.sxpcwlkj.entity.vo.StoreMemberAddressVo;
import com.sxpcwlkj.sxpcwlkj.entity.export.StoreMemberAddressExport;
import com.sxpcwlkj.sxpcwlkj.service.StoreMemberAddressService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 会员收货地址
 *
 * @author 西决
 * @Doc <a href='https://www.mmsadmin.com'>MMS文档</a>
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("sxpcwlkj/storeMemberAddress")
public class StoreMemberAddressController extends BaseController{
    private final StoreMemberAddressService baseService;

    /**
    * 分页列表-会员收货地址
    * @param bo 查询条件
    * @return 分页对象
    */
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:list")
    @PostMapping("/list")
    public TableDataInfo<StoreMemberAddressVo> listPage(@RequestBody @Validated(ValidatedGroupConfig.query.class) StoreMemberAddressBo bo){
        return baseService.selectListVoPage(bo, bo.getPageQuery());
    }

    /**
    * 根据id查询-会员收货地址
    * @param id ID
    * @return 对象
    */
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:query")
    @GetMapping("/{id}")
    public R<StoreMemberAddressVo> queryById(@PathVariable String id) {
        return R.success(baseService.selectVoById(id));
    }

    /**
    * 修改-会员收货地址
    * @param bo 对象
    * @return true:成功 false:失败
    */
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:edit")
    @PutMapping
    public R<Boolean> edit(@RequestBody @Validated(ValidatedGroupConfig.update.class) StoreMemberAddressBo bo) {
        return R.success(baseService.updateById(bo));
    }

    /**
    * 新增-会员收货地址
    * @param bo 对象
    * @return true:成功 false:失败
    */
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:insert")
    @PostMapping
    public R<Boolean> insert(@RequestBody @Validated(ValidatedGroupConfig.insert.class) StoreMemberAddressBo bo) {
        return R.success(baseService.insert(bo));
    }

    /**
    * 删除-会员收货地址
    * @param ids ID
    * @return true:成功 false:失败
    */
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:delete")
    @DeleteMapping("/{ids}")
    public R<Boolean> delete(@PathVariable String ids) {
        return R.success(baseService.deleteById(ids));
    }

    /**
    * 模版下载-会员收货地址
    */
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil.download(response, StoreMemberAddressExport.class, "会员收货地址");
    }

    /**
    * 导入-会员收货地址
    * @param file 模版文件
    */
    @MssSafety
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:import")
    @PostMapping("/import")
    public R<Boolean> imports(@RequestParam("file") MultipartFile file) throws Exception {
        Set<StoreMemberAddressExport> list=  ExcelUtil.imports(file, StoreMemberAddressExport.class);
        Boolean state= baseService.imports(list);
        return R.ok(state,state?"数据导入成功!":"数据导入失败!");
    }

    /**
    * 导出-会员收货地址
    */
    @MssSafety
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:export")
    @PostMapping("/export")
    public void export(@Validated(ValidatedGroupConfig.query.class) StoreMemberAddressBo bo,HttpServletResponse response) throws IOException {
        List<StoreMemberAddressVo> list= baseService.selectListVoPage(bo, bo.getPageQuery()).getRows();
        List<StoreMemberAddressExport> data= MapstructUtil.convert(list,StoreMemberAddressExport.class);
        ExcelUtil.export(response, StoreMemberAddressExport.class, "会员收货地址",data);
    }

    /**
    * 打印-会员收货地址
    */
    @MssSafety
    @SaCheckPermission("sxpcwlkj:storeMemberAddress:print")
    @PostMapping("/print")
    public R<PrintObject<StoreMemberAddressExport>> print(@RequestBody @Validated(ValidatedGroupConfig.query.class) StoreMemberAddressBo bo) throws Exception {
        List<StoreMemberAddressVo> list= baseService.selectListVoPage(bo, bo.getPageQuery()).getRows();
        List<StoreMemberAddressExport> data= MapstructUtil.convert(list,StoreMemberAddressExport.class);
        PrintObject<StoreMemberAddressExport>  printObject=   new PrintObject<StoreMemberAddressExport>()
             .setTitle("会员收货地址")
             .setData(data);
             return R.response(Boolean.TRUE,printObject);
    }
}
